package com.ylz.sjzx.sio.room.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.ylz.sjzx.sio.common.mid.cache.redis.JedisTemplate;
import com.ylz.sjzx.sio.common.core.constant.Const;
import com.ylz.sjzx.sio.room.entity.DisableUser;
import com.ylz.sjzx.sio.room.entity.DisableUserInfo;
import com.ylz.sjzx.sio.room.entity.Notice;
import com.ylz.sjzx.sio.room.entity.Question;
import com.ylz.sjzx.sio.room.entity.User;
import com.ylz.sjzx.sio.sdk.client.session.IMSession;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/*
 * 正式场景下，使用redis或者数据库来存储session信息
 */
public final class RoomRepository {

    /**
     * 禁言列表
     *
     * @param serssion
     * @return
     */
    @SneakyThrows
    public static Map denyList(IMSession serssion) {
        return JedisTemplate.me()
                .hgetAll(Const.PREFIX_YKT + serssion.getRoomName() + ":denyList");
    }

    /**
     * 存储聊天室在线用户
     *
     * @param session
     * @param user
     */
    @SneakyThrows
    public static void saveOnlineList(IMSession session, User user) {
        JedisTemplate.me()
                .hSetOrigin(Const.PREFIX_YKT + session.getRoomName() + ":onlineList", session.getUserId().toString(), user);
    }

    /**
     * 获取在线人员名单
     *
     * @param serssion
     */
    @SneakyThrows
    public static Map onlineList(IMSession serssion) {
        return JedisTemplate.me()
                .hgetAll(Const.PREFIX_YKT + serssion.getRoomName() + ":onlineList");
    }

    /**
     * 保存组聊天室组用户
     *
     * @param serssion
     */
    @SneakyThrows
    public static void save2RoomGroup(IMSession serssion) {

        if (serssion.isStudent()) {
            JedisTemplate.me()
                    .hSet(Const.PREFIX_YKT + serssion.getRoomName() + ":" + serssion.getGroupName(),
                            serssion.getUserId().toString(), serssion.getSid());
        } else {
            JedisTemplate.me()
                    .hSet(Const.PREFIX_YKT + serssion.getRoomName() + ":admin",
                            serssion.getUserId().toString(), serssion.getSid());
        }

    }

    /**
     * 删除聊天室组用户
     *
     * @param serssion
     */
    @SneakyThrows
    public static void removeFromRoomGroup(IMSession serssion) {
        if (serssion.isStudent()) {
            JedisTemplate.me()
                    .hashDel(Const.PREFIX_YKT + serssion.getRoomName() + ":" + serssion.getGroupName(),
                            Arrays.asList(serssion.getUserId().toString()));
        } else {
            JedisTemplate.me()
                    .hashDel(Const.PREFIX_YKT + serssion.getRoomName() + ":admin",
                            Arrays.asList(serssion.getUserId().toString()));
        }

    }

    /**
     * 禁言用户
     *
     * @param serssion
     * @return
     */
    @SneakyThrows
    public static String deny(IMSession serssion) {
        return JedisTemplate.me()
                .hashGet(Const.PREFIX_YKT + serssion.getRoomName() + ":denyList", serssion.getUserId().toString(),
                        String.class);

    }

    /**
     * 班级禁言
     *
     * @param serssion
     * @return
     */
    @SneakyThrows
    public static String denyClass(IMSession serssion) {
        return JedisTemplate.me()
                .get(Const.PREFIX_YKT + serssion.getRoomName() + ":disableSend",
                        String.class);

    }

    /**
     * 公告
     *
     * @param serssion
     * @return
     */
    @SneakyThrows
    public static String notice(IMSession serssion) {
        return JedisTemplate.me()
                .get(Const.PREFIX_YKT + serssion.getRoomName() + ":notice",
                        String.class);

    }

    /**
     * 倒时器
     *
     * @param serssion
     * @return
     */
    @SneakyThrows
    public static String countdown(IMSession serssion) {
        return JedisTemplate.me()
                .get(Const.PREFIX_YKT + serssion.getRoomName() + ":countdown",
                        String.class);

    }


    /**
     * 获取在线学生信息
     *
     * @param session
     * @param userIdStr
     * @param s
     * @return
     * @throws Exception
     */
    public static DisableUserInfo getStudentUserInfo(IMSession session, String userIdStr, String s) throws Exception {
        Object student = JedisTemplate.me().hashGet(Const.PREFIX_YKT + session.getRoomName() + s,
                userIdStr);
        if (student != null) {
            DisableUserInfo disableUserInfo = new DisableUserInfo();
            BeanUtil.copyProperties(student, disableUserInfo);
            return disableUserInfo;
        }
        return null;
    }

    /**
     * 保存禁言名单
     *
     * @param session
     * @param student
     * @throws Exception
     */
    public static void saveDenyList(IMSession session, DisableUserInfo student) throws Exception {
        JedisTemplate.me().hSet(Const.PREFIX_YKT + session.getRoomName() + ":denyList",
                student.getUserId().toString(), student);
    }

    /**
     * 删除禁言名单
     *
     * @param session
     * @param list
     * @throws Exception
     */
    public static void deleteDenyList(IMSession session, List<String> list) throws Exception {
        JedisTemplate.me().hashDel(Const.PREFIX_YKT + session.getRoomName() + ":denyList", list);
    }

    public static void deleteDisableSend(IMSession session) throws Exception {
        JedisTemplate.me().set(Const.PREFIX_YKT + session.getRoomName() + ":disableSend", "");
    }

    public static void deleteDisableSend(IMSession session, DisableUser disableUser) throws Exception {
        JedisTemplate.me()
                .set(Const.PREFIX_YKT + session.getRoomName() + ":disableSend", disableUser);
    }

    /**
     * 保存问题
     *
     * @param session
     * @param question
     * @param id
     * @throws Exception
     */
    public static void saveQuestion(IMSession session, Question question, Long id) throws Exception {
        if (CollectionUtil.isEmpty(question.getChildren())) {
            question.setChildren(new ArrayList<>());
        }
        JedisTemplate.me().hSet(Const.PREFIX_YKT + session.getRoomName() + ":question", id.toString()
                , question);
    }

    /**
     * 获取问题
     *
     * @param session
     * @param field
     * @return
     * @throws Exception
     */
    public static Question getQuestion(IMSession session, String field) throws Exception {
        return JedisTemplate.me().hashGet(Const.PREFIX_YKT + session.getRoomName() + ":question", field,
                Question.class);
    }

    /**
     * 获取问题索引
     *
     * @param session
     * @return
     * @throws Exception
     */
    public static Long getQuestionIndex(IMSession session) throws Exception {
        return JedisTemplate.me().incr(Const.PREFIX_YKT + session.getRoomName() + ":questionIndex", 1);
    }

    /**
     * 保存公告
     *
     * @param session
     * @param notice
     * @throws Exception
     */
    public static void saveNotice(IMSession session, Notice notice) throws Exception {
        JedisTemplate.me().set(Const.PREFIX_YKT + session.getRoomName() + ":notice", notice);
    }

}
